<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Upload extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
   public function images(){
    if(!$this->request->isPost()){
          return error_json('非法请求');
    }
     //数据安全过滤
     $param = $this->request->param();
   
    //  $header = $this->request->header();
    //  if(empty($header['token'])){
    //      return error_json('请求 Token 参数不存在');  
    //  }
 
    //  if($header['token'] != session('__token__')){
    //      return error_json('请勿重复提交，请刷新后操作');
    //  }  
 
    //  if(!$this->request->file('files')){
    //      return error_json('文件上传接口：请求参数错误');
    //  }

    if(!$this->request->file('images')){ return error_json('图片上传接口：请求参数错误');}

    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files    = $this->request->file('images');
    // 移动到框架应用根目录/uploads/ 目录下

     $images_path = !empty($settings['attachments']['images_path'])?$settings['attachments']['images_path']:config('app.upload_images.path');  

    $images_ext  = !empty($settings['attachments']['images_ext'])?$settings['attachments']['images_ext']:config('app.upload_images.ext');
    $images_size = !empty($settings['attachments']['images_size'])?$settings['attachments']['images_size']:config('app.upload_images.size');


    foreach($files as $file){
 
          $info = $file->validate(['size'=> $images_size,'ext'=>$images_ext])->move(QL_ROOT.$images_path);
        if($info){
           $images =  $file->getInfo('images');



           return success_json('上传成功','/'.trim($images_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
               return error_json($file->getError());
        }

    }


 }

 //上传头像
public function avatar(){
    if(!$this->request->isPost()){
          return error_json('非法请求');
    }
     //数据安全过滤
    $param = $this->request->param();

    if(!$this->request->file('image')){ return error_json('图片上传接口：请求参数错误');}
    $settings = $this->settings;

 
    // 获取表单上传文件 例如上传了001.jpg
    $file    = $this->request->file('image');
    // 移动到框架应用根目录/uploads/ 目录下
     $images_path = !empty($settings['attachments']['images_path'])?$settings['attachments']['images_path']:config('app.upload_images.path');  

    $images_ext  = !empty($settings['attachments']['images_ext'])?$settings['attachments']['images_ext']:config('app.upload_images.ext');
    $images_size = !empty($settings['attachments']['images_size'])?$settings['attachments']['images_size']:config('app.upload_images.size');
        $name = session('uid').'.png';
        $images_path = $images_path.'avatar/';
        $info = $file
        ->rule('uniqid')
        ->validate(['size'=> $images_size,'ext'=>$images_ext])
        ->move(QL_ROOT.$images_path,$name);
        if($info){
            $images =  $file->getInfo('image');


              
                   return success_json('上传成功','/'.trim($images_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
             // 上传失败获取错误信息
                       return error_json($file->getError()); 
        }


  



 }

 public function videos(){
    if(!$this->request->isPost()){
          return error_json('非法请求');
    }
     //数据安全过滤
    $param = $this->request->param();
     //数据安全过滤
     $header = $this->request->header();

     $param = $this->request->param();
     if(empty($header['token'])){
         return error_json('请求 Token 参数不存在');  
     }
 
     if($header['token'] != session('__token__')){
         return error_json('请勿重复提交，请刷新后操作');
     }  

   if(!$this->request->file('videos')){ return error_json('视频上传接口：请求参数错误');}
    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('videos');
    // 移动到框架应用根目录/uploads/ 目录下
    // print_r($files);
    $videos_path = !empty($settings['attachments']['videos_path'])?$settings['attachments']['videos_path']:config('app.upload_videos.path');
    $videos_ext = !empty($settings['attachments']['videos_ext'])?$settings['attachments']['videos_ext']:config('app.upload_videos.ext');
    $videos_size = !empty($settings['attachments']['videos_size'])?$settings['attachments']['videos_size']:config('app.upload_videos.size');

    foreach($files as $file){
          $info = $file->validate(['size'=> $videos_size,'ext'=>$videos_ext])->move(QL_ROOT.$videos_path);
        if($info){
           $videos =  $file->getInfo('videos');

            session('upload_token',null);  //清空session token
             return success_json('上传成功','/'.trim($videos_path,'/').'/'.str_replace('\\','/',$info->getSaveName()));
        }else{
            // 上传失败获取错误信息
             return error_json($file->getError());
        }

    }


 }

//上传附件
 public function files(){
    if(!$this->request->isPost()){
        return $this->error('非法访问');
    }

    $param = $this->request->param();

     //数据安全过滤
      $header = $this->request->header();
    if(empty($header['token'])){
        return error_json('请求 Token 参数不存在');  
    }

    if($header['token'] != session('__token__')){
        return error_json('请勿重复提交，请刷新后操作');
    }  

    if(!$this->request->file('files')){
        return error_json('文件上传接口：请求参数错误');
    }



    $settings = $this->settings;
    // 获取表单上传文件 例如上传了001.jpg
    $files = $this->request->file('files');
    // 移动到框架应用根目录/uploads/ 目录下

    $files_path = !empty($settings['attachments']['files_path'])?$settings['attachments']['files_path']:config('app.upload_files.path');
    $files_ext = !empty($settings['attachments']['files_ext'])?$settings['attachments']['files_ext']:config('app.upload_files.ext');
    $files_size = !empty($settings['attachments']['files_size'])?$settings['attachments']['files_size']:config('app.upload_files.size');

    foreach($files as $file){
          $info = @$file->validate(['size'=> $files_size,'ext'=>$files_ext])->move(QL_ROOT.$files_path);
          
        if(!empty($info)){
           $files =  $file->getInfo('files');
            // 成功上传后 获取上传信息

            $id  = !empty($param['id'])?intval($param['id']):0;
          
             $path = str_replace("\\",'/',$files_path.$info->getSaveName());

             return success_json('上传成功',$path);
        }else{
            // echo $file->getError();
            // 上传失败获取错误信息
           return error_json($file->getError());
        }

    }


 }
}